package com.jinzhi.jinzhidata.service.impl;

import com.jinzhi.jinzhidata.enums.ResourceStateEnum;
import com.jinzhi.jinzhidata.vo.ListResourceVO;
import com.jinzhi.jinzhidata.dataobject.Resource;
import com.jinzhi.jinzhidata.repository.ResourceRepository;
import com.jinzhi.jinzhidata.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description
 * @date 2019-12-29 13:17
 **/
@Service
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    ResourceRepository resourceRepository;

    @Override
    public List<ListResourceVO> selectResourceListByType(String resourceTypeName, PageRequest pageRequest) {
        String resourceSate = ResourceStateEnum.CHECK_PASS.getCode();
        if (resourceTypeName == null) {
            return resourceRepository.selectResourceList(resourceSate);
        }
        return resourceRepository.selectResourceListByType(resourceSate, resourceTypeName, pageRequest);
    }

    @Override
    public List<ListResourceVO> searchResourceByName(String resourceName, PageRequest pageRequest) {
        String resourceSate = ResourceStateEnum.CHECK_PASS.getCode();
        return resourceRepository.searchResourceByName(resourceSate, resourceName, pageRequest);
    }

    @Override
    public Resource selectByResourceId(String resourceId) {
        return resourceRepository.findById(resourceId).get();
    }

    @Override
    public Resource saveResource(Resource resource) {
        return resourceRepository.save(resource);
    }

    @Override
    public List<ListResourceVO> selectResourceListByAuthor(String authorId) {
        return resourceRepository.selectResourceListByAuthor(authorId);
    }

    @Override
    public List<ListResourceVO> selectUserBuyResourceList(List<String> resourceIdList) {
        return resourceRepository.selectUserBuyResourceList(resourceIdList);
    }

    @Override
    public Long count() {
        return resourceRepository.count();
    }

    @Override
    public List<ListResourceVO> selectResourceList(PageRequest pageRequest) {
        return resourceRepository.selectResourceList();
    }

    @Override
    public Resource checkResource(String resourceId, String resourceState) {
        Resource resource = selectByResourceId(resourceId);
        resource.setResourceState(resourceState);
        return resourceRepository.save(resource);
    }
}
